import 'package:baselib/utils/drawable_base_util.dart';
import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:framelib/config/app_config.dart';
import 'package:framelib/net/base_entity.dart';
import 'package:framelib/net/request_manage.dart';

class CachedNetworkImageWidget extends StatelessWidget {
  String? imgKey;
  String? imgNetPath;
  String? imgRequestUrl;
  BorderRadius? radius;
  Color? color;
  String? placeholder; double? placeWidth;
  double? width; double? height;
  BoxFit? fit;String? package;Color? placeholderColor;
   CachedNetworkImageWidget({super.key,
    this.imgKey,this.imgNetPath,this.imgRequestUrl,
    this.radius,this.color,this.placeholder,this.placeWidth,this.width,this.height,
    this.fit,this.package,this.placeholderColor
  }){
     if((imgKey??'').isNotEmpty){
       getFilePathByFileKey(fileKey: imgKey!);
     }else{
       filePathNotifier.value = (imgNetPath??'');
     }
   }

  /**
   * 可根据minio的filekey获取文件路径
   */
  getFilePathByFileKey({required String fileKey})async{
    String getFileByFileKey = imgRequestUrl??'/file/getFileByFileKey';
    BaseEntity baseEntity = await RequestManage.instance.get(
        getFileByFileKey,
        isFormData: false,
        queryParameters: {'fileKey':fileKey},
        fromJsonFunc: (json)=>json
    );
    if(baseEntity?.code == AppConfig.instance.SUCCESS_CODE){
      // LogUtil.d('getUploadUrl-->baseEntity=${baseEntity?.toJson()}');
      filePathNotifier.value = (baseEntity?.data??'');
    }
  }
  ValueNotifier<String> filePathNotifier = new ValueNotifier('');

  @override
  Widget build(BuildContext context) {
    if(placeholder==null)placeholder = 'none';
    return ValueListenableBuilder(
        valueListenable: filePathNotifier,
        builder: (context,filePath,child){
          return Container(
            width: width,
            height: height,
            child: ClipRRect(
                borderRadius: radius??BorderRadius.zero,
                child: CachedNetworkImage(
                  key: key,
                  imageUrl: filePath??'',
                  color: color,
                  placeholder: (context, url) => (placeholder=='none')?CupertinoActivityIndicator()
                      :ImageUtil.loadAssetImage(placeholder??'none', height: height, width: width, fit: fit??BoxFit.cover,color: placeholderColor,package: package),
                  errorWidget: (context, url, error) => (placeholder=='none')?
                  Container(child:
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ImageUtil.loadAssetImage(
                          drawable_base_icon_empty,
                          isOrigin: true,
                          width: placeWidth??px_base_150.w,
                          fit: BoxFit.fitWidth,
                          color: placeholderColor
                      )
                    ],
                  ),
                  ):
                  ImageUtil.loadAssetImage(
                      placeholder??'none',color: placeholderColor, height: height, width: width, fit: fit??BoxFit.cover,package: package
                  ),
                  width: width,
                  height: height,
                  fit: fit??BoxFit.cover,
                )
            ),
          );
        }
    );
  }
}
